package com.nomi.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nomi.blog.entity.Following;
import com.nomi.blog.mapper.FollowingMapper;
import com.nomi.blog.service.FollowingService;
import com.nomi.blog.service.UserInfoService;
import com.nomi.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FollowingServiceImpl implements FollowingService {
    @Autowired
    private FollowingMapper followingMapper;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 关注用户
     * @param userId
     * @param followingUserId
     * @return
     */
    @Override
    @Transactional
    public Result followUser(Long userId, Long followingUserId) {
        QueryWrapper<Following> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("following_user_id", followingUserId);
        Following existingFollowing = followingMapper.selectOne(queryWrapper);
        if(existingFollowing != null){
            return Result.fail("你已经关注了该用户");
        }
        Following following = new Following();
        following.setUserId(userId);
        following.setFollowingUserId(followingUserId);
        int result = followingMapper.insert(following);
        if (result > 0){
            userInfoService.updateUserFollowingCount(userId, 1);
            // 更新作者粉丝数
            userInfoService.updateUserFollowerCount(followingUserId, 1);
            return Result.success("关注成功");
        } else {
            return Result.fail("关注失败");
        }
    }

    /**
     * 取消关注用户
     * @param userId
     * @param followingUserId
     * @return
     */
    @Override
    @Transactional
    public Result unfollowUser(Long userId, Long followingUserId) {
        QueryWrapper<Following> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("following_user_id", followingUserId);
        int result = followingMapper.delete(queryWrapper);
        if (result > 0){
            // 更新用户关注数
            userInfoService.updateUserFollowingCount(userId, -1);
            // 更新作者粉丝数
            userInfoService.updateUserFollowerCount(followingUserId, -1);
            return Result.success("取消关注成功");
        } else {
            return Result.fail("取消关注失败");
        }
    }

    /**
     * 获取用户关注列表
     * @param userId
     * @return
     */
    @Override
    public Result getFollowingList(Long userId) {
        QueryWrapper<Following> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Following> followingList = followingMapper.selectList(queryWrapper);
        return Result.success("关注列表获取成功", followingList);
    }


    /**
     * 更新用户关注数
     * @param userId
     * @param increment
     * @return
     */
    @Override
    @Transactional
    public Result updateFollowingCount(Long userId, int increment) {
        return userInfoService.updateUserFollowingCount(userId, increment);
    }
}
